#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: tangzhichao

from typing import List, Optional
from langchain.text_splitter import RecursiveCharacterTextSplitter
from .base import BaseSlicer


class RecursiveSlicer(BaseSlicer):
    """基于递归字符的分割器实现"""

    def __init__(
            self,
            chunk_size: int = 500,
            chunk_overlap: int = 50,
            separators: Optional[List[str]] = None
    ):
        """
        初始化分割器
        :param chunk_size: 每个切片的字符数
        :param chunk_overlap: 切片间的重叠字符数
        :param separators: 自定义分隔符列表
        """
        self.splitter = RecursiveCharacterTextSplitter(
            chunk_size=chunk_size,
            chunk_overlap=chunk_overlap,
            separators=separators or ["\n\n", "\n", "。", "！", "？", "；", " ", ""]
        )

    def split(self, text: str) -> List[str]:
        """
        执行文本分割
        :param text: 输入文本
        :return: 分割后的文本片段列表
        """
        return self.splitter.split_text(text)

    @property
    def strategy_name(self) -> str:
        return "recursive"